#!/usr/bin/perl -w
# Used to pipe tagged sentences into the NP Chunker and pipe out
# chunked text

use strict;
use warnings;

use IPC::Open3;
use IO::Select;

my $rio = IO::Select->new();
my $eio = IO::Select->new();

my($rdr, $wrt, $err);

my($pid) = 0;
sub npPipeBegin
{
	my $dir = "NP Chunker";
	chdir $dir;
	$pid = open3($wrt, $rdr, $err, './chunker.sh') or die "ERROR:  Couldn't call open3\n";
		#"perl $dir/tmp.pl -d $dir/HONORIFICS");
	chdir "..";
	
	## add the reader and the err file handles to their own IO::Selects
	## so that we can easily determine later whether they are ready to be read
	$rio->add($rdr);
	$eio->add($err);
	
	## turn on autoflushing for the writer file handle
	select($wrt);
	$| = 1;
	select(STDOUT);
	
	## check for any errors
	if(npCanReadErr())
	{
		print "An error arose!\n";
		exit;
	}
	
	#print "Init done\n";
}

sub npPipeClose
{
	close $wrt;
	close $rdr;
	#close $err;
}

sub npWrite
{
	my $msg = shift;
	#print "Got message '$msg'\n";
	print $wrt $msg;
}

## for npCanRead* calls, we require a timeout (passed or default);
sub npCanRead
{
	# get optional timeout length
	my $len = shift;
	if(!defined($len)){	$len = 0.1;	}
	
	my @readable = $rio->can_read($len);
	
	#print "Readable:  $readable[0]\n";
	
	# see whether np's STDIN is ready for read
	if(@readable)
	{
		#print "readable (STDIN,YES): {{$#readable}}\n";
		return 1;
	}
	else
	{
		#print "readable (STDIN,NO): {{$#readable}}\n";
	}
	return 0;
}
sub npRead
{
	# timeout wait
	my $timeout = shift;
	
	my $msg = undef;
	if(npCanRead($timeout))
	{
		$msg = <$rdr>;
		#$msg = readline($rdr);
	}
	return $msg;
}
sub npCanReadErr
{
	# get optional timeout length
	my($len);
	$len = shift;
	if(!defined($len)){$len = 0.1;}
	
	my @readable = $eio->can_read($len);
	# see whether np's STDERR is ready for read
	if(@readable)
	{
		#print "readable (STDERR,YES): {{$#readable}}\n";
		return 1;
	}
	else
	{
		#print "readable (STDERR,NO): {{$#readable}}\n";
	}
	return 0;
}
sub npReadErr
{
	#timeout wait
	my $timeout = shift;
	
	my $msg = undef;
	if(npCanReadErr($timeout))
	{
		$msg = <$err>;
	}
	return $msg;
}

sub npTest
{
	print "Begin Test\n";
	
	my $testmsg = "The/DT quick/JJ brown/JJ fox/NN jumped/VBD over/IN the/DT fat/JJ lazy/JJ dog/NN ./.\n" .
					"Hello/RB ,/, what/WP 's/VBZ your/PRP\$ name/NN ?/.\n" .
					"My/PRP\$ name/NN is/VBZ Bob/NNP ./.\n";

	my $read = "";

	while(npCanRead(1) || npCanReadErr(1))
	{
		#while(npCanRead())
		#{
		#	print "Some stuff could be read: '";
		#	print $read if ($read = npRead());
		#	print "'\n";
		#}
		#while(npCanReadErr())
		#{
		#	print "Some stuff could be read from Err: '";
		#	print $read if ($read = npReadErr());
		#	print "'\n";
		#}
		if(postCanRead(0.1))
		{
			print "Can read STDIN: \"" . postRead() . "\"\n";
		}
		if(postCanReadErr(0.1))
		{
			print "Can read STDERR: \"" . postReadErr() . "\"\n";
		}	
	}
	
	print "Trying to write...\n";
	
	npWrite($testmsg);
	
	print "Trying to read...\n";
	
	for (1..3)
	{
		$read = "";
		if(!($read = npRead(10)))
		{
			print "Could not read on loop $_\n";
		}
		else
		{
			print "read: '$read'\n";
		}
	}
	
	#close $wrt;
	#close $rdr;
	#close $err;
	
	print "Test Done!\n";
}
npPipeBegin();

#npTest();

1;  # stops perl from complaining

